package cn.itsource.ymcc.web.controller;

import cn.itsource.ymcc.constant.RConst;
import cn.itsource.ymcc.dto.KillDto;
import cn.itsource.ymcc.service.IKillCourseService;
import cn.itsource.ymcc.domain.KillCourse;
import cn.itsource.ymcc.query.KillCourseQuery;
import cn.itsource.ymcc.result.JsonResult;
import cn.itsource.ymcc.result.PageList;
import cn.itsource.ymcc.utils.jwt.UserContext;
import cn.itsource.ymcc.vo.KillVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/killCourse")
public class KillCourseController {

    @Autowired
    public IKillCourseService killCourseService;

    @GetMapping("/getKillCourse")
    public JsonResult getKillCourse(){
        return killCourseService.getKillCourse();
    }

    @GetMapping("/getDetailKillCourse/{id}")
    public JsonResult getDetailKillCourse(@PathVariable("id")Long id){
        return JsonResult.success().setData(killCourseService.getDetailKillCourse(id));
    }

    @PostMapping("/kill")
    public JsonResult kill(@RequestBody @Valid KillVo killVo, HttpServletRequest request){
        //取用户id
        Long userId = UserContext.getUserId(request.getHeader("Token"));
        KillDto killDto = new KillDto();
        BeanUtils.copyProperties(killVo,killDto);
        killDto.setLoginId(userId);
        return killCourseService.kill(killDto);
    }

    /**
    * 保存和修改公用的
    */
    @PostMapping("/save")
    public JsonResult saveOrUpdate(@RequestBody KillCourse killCourse){
        if(killCourse.getId()!=null){
            killCourseService.updateById(killCourse);
        }else{

            killCourseService.save(killCourse);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        killCourseService.removeById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success().setData(killCourseService.getById(id));
    }


    /**
    * 查询所有对象
    */
    @GetMapping("/list")
    public JsonResult list(){
        return JsonResult.success().setData(killCourseService.list(null));
    }


    /**
    * 带条件分页查询数据
    */
    @PostMapping("/pagelist")
    public JsonResult page(@RequestBody KillCourseQuery query){
        Page<KillCourse> page = new Page<KillCourse>(query.getPageNo(), query.getPageSize());
        page = killCourseService.page(page);
        return JsonResult.success().setData(new PageList<KillCourse>(page.getTotal(), page.getRecords()));
    }
}
